<?php

/**
 * multichat actions.
 *
 * @package    alika
 * @subpackage multichat
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class multichatActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
     $c = new Criteria();
     $c->add(ChatroomPeer::FROM_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
     $c->add(ChatroomPeer::IS_ACTIVE,true);
     $this->active_room = ChatroomPeer::doSelectOne($c);
  }

    public function executeCall(sfWebRequest $request)
  {
    $c = new Criteria();
    $c->add(ChatroomPeer::FROM_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
     $c->add(ChatroomPeer::TO_ID,$request->getParameter('id'));
     $cnt = ChatroomPeer::doCount($c);
    if($cnt==0)
    {
        $c = new Criteria();
     $c->add(ChatroomPeer::FROM_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
     $rs = ChatroomPeer::doSelect($c);
     foreach ($rs as $r)
         {
         $r->setIsActive(false);
         $r->save();
         }
    
    $room = new Chatroom();
    $room->setFromId($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
    $room->setToId($request->getParameter('id'));
    $room->setIsActive(true);
    $room->save();
    }
    $this->redirect('multichat/index');
  }

    public function executeExit(sfWebRequest $request)
  {
    $room = ChatroomPeer::retrieveByPK($request->getParameter('id'));
    if($room)
    $room->delete();
    $this->redirect('multichat/index');
  }

   public function executeSetactive(sfWebRequest $request)
  {


    $room = ChatroomPeer::retrieveByPK($request->getParameter('id'));
    if($room)
    {
     $c = new Criteria();
     $c->add(ChatroomPeer::FROM_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
     $rs = ChatroomPeer::doSelect($c);
     foreach ($rs as $r)
         {
         $r->setIsActive(false);
         $r->save();
         }

    $room->setIsActive(true);
    $room->save();
    }
    $this->redirect('multichat/index');
  }

}
